const args = require("minimist")(process.argv.slice(2));

import { uglify } from 'rollup-plugin-uglify';
const { resolve } = require("path");
//基于esbuild构建
const {build}=require('esbuild')
//解析命令行参数
console.log(args);

const target = args._[0] || "reactivity";
const format = args.f || "global";

//开发工具直只打包某一个
//获取包中的package.json文件
const pkg = require(resolve(__dirname, `../packages/${target}/package.json`));

//获取打包的类型
/**
 * iife 立即执行函数
 * cjs node中的模块
 * esm 浏览器中的es6
 */
const formatObj = {
  global: "iife",
  cjs: "cjs",
  "esm-bundler": "esm",
};

const outputFormat=formatObj[format]

//打包的路径

const outfile=resolve(__dirname,`../packages/${target}/dist/${target}.${format}.js`)


// 打包
build({
    entryPoints:[resolve(__dirname,`../packages/${target}/src/index.ts`)],
    outfile,
    bundle:true,
    // sourcemap:true,
    format:outputFormat,
    globalName:pkg.buildOPtions?.name,
    platform:format==='cjs'?'node':'browser',
    watch:{
        //监听文件的变化
        onRebuild(error){
            if(!error){
                console.log('rebuild');
            }
        }
    },
    plugins:[uglify()]
}).then(()=>{
    console.log('watching.....');
})